<?php
class SoftwareDescription
{

    private $property = array();
    private $valid = false;

    private $valid_properties = "Name,Description";

    public static function createNew($name, $description)
    {
        $S = new SoftwareDescription();
        $S->Description($description);
        $S->Name($name);

        return $S;
    }

    public static function createFromArray($values)
    {
        if (!is_array($values)) return null;
        if (count($values) <= 0) return null;

        $S = new SoftwareDescription();
        $S->FromArray($values);
        return $S;
    }

    public static function createFromCommand(command $cmd)
    {
        logger::called();

        if (!is_array($cmd->Data())) return null;
        if (count($cmd->Data()) <= 0) return null;

        $S = new SoftwareDescription();
        $S->FromArray($cmd->Data());
        return $S;
    }
    
    public function __construct()
    {

    }

    public function ID()
    {
        return $this->GetSetData("SoftwareID",null);
    }
    
    public function Name($value = null)
    {
        return $this->GetSetData("SoftwareName",$value);
    }
    
    public function Description($value = null)
    {        
        return $this->GetSetData("SoftwareDescription",$value);
    }

    public function Category($value = null)
    {        
        return $this->GetSetData("Category",$value);
    }
    
    public function Owner($value = null)
    {
        return $this->GetSetData("OwnerUsername",$value);
    }    
    
    public function Approved($value = null)
    {
        return $this->GetSetData("Approved",$value);
    }
    
    public function AvatarImage($value = null)
    {
        return $this->GetSetData("AvatarImage",$value);
    }

    public function UpdateAvatarImage($postedFile)
    {
        $path_parts = pathinfo($postedFile["name"]);
        $ext = $path_parts['extension'];
        $newFilename = $this->ID() . "-avatar." . $ext;
        $newPathName = self::AvatarImagePath($newFilename);

        @move_uploaded_file($postedFile['tmp_name'], $newPathName);
        if (!file::reallyExists($newPathName)) return FALSE;

        $updated = ns_dao_software::updateSoftware($this->ID(), "AvatarImage", "");
        $updated = ns_dao_software::updateSoftware($this->ID(), "AvatarImage", $newFilename);
        
        $this->AvatarImage($newFilename);
        return $updated;
    }
    
    public static function AvatarImagePath($fileName) {
        return "../../content/softwareAvatar/" . $fileName;
        //return configuration::$APPLICATION_FOLDER . "/content/softwareAvatar/" . $fileName;
        //return "../../content/softwareAvatar/" . $fileName;
    }

    public function StorableData()
    {
        $result["SoftwareName"] = $this->Name();
        $result["SoftwareDescription"] = $this->Description();
        $result["OwnerUsername"] = 1;

        return $result;
    }

    public function UpdateSoftwareToDatabase()
    {
        $result = ns_dao_software::updateSoftwareByArray($this->property["SoftwareID"], $this->property);
        return $result;
    }

    private function GetSetData($property_name,$value = null)
    {
        //if (!$this->checkPropertyName($property_name)) return null;
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    private function checkPropertyName($property_name)
    {
        foreach (explode(",",$this->valid_properties) as $allowed_prop)
            if ($allowed_prop == $property_name) return true;

        return false;
    }


    /*
     * Populate from an array
     */
    public function FromArray($values)
    {
        if (!is_array($values)) return null;

        foreach ($values as $key => $value)
            $this->GetSetData($key,$value);

    }


    public function __toString()
    {
        return print_r($this->property,true);
    }


}

?>
